<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-if="false" v-model="listQuery.lineNumber" :placeholder="$t('equipment.lineNumber')" class="filter-item" style="width: 200px" @change="handleFilter">
        <el-option v-for="item in lineOptions" :key="item.lineNumber" :label="item.lineName" :value="item.lineNumber" />
      </el-select>
    </div>
    <el-row style="background:#fff;margin-bottom:32px;">
      <line-chart :chart-data="lineChartData" />
    </el-row>
    <el-row style="background:#fff;margin-bottom:32px;">
      <line-chart :chart-data="lineChartData2" />
    </el-row>
  </div>
</template>

<script>
import { getProcessCycle, getLineList, getProcessList } from '@/api/equipment'
import waves from '@/directive/waves' // waves directive
import LineChart from '@/components/Charts/LineChart.vue'
import { parseTime } from '@/utils'
import i18n from '@/lang'

export default {
  name: 'CycleTime',
  components: {
    LineChart },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      editRight: true,
      tableKey: 0,
      list: [],
      lineOptions: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        state: 0,
        lineNumber: '',
        startTime: parseTime(Date.now(), '{y}-{m}-{d} 00:00:00'),
        endTime: '',
        sort: '+due_date'
      },
      lineChartData: {
        valueX: [],
        valueY: [],
        title: i18n.t('equipment.cycleTime')
      },
      lineChartData2: {
        valueX: [],
        valueY: [],
        title: i18n.t('equipment.cycleTimePerHour')
      }
    }
  },
  created() {
    this.editRight = (this.$store.getters.roles === 'admin' || this.$store.getters.roles === 'administrator')
    this.listQuery.lineNumber = this.$store.getters.lineNumber
    this.getLineList()
  },
  methods: {
    formatDateTime(date) {
      return date.substring(0, 10)
    },
    // 获取线体清单
    getLineList() {
      this.listLoading = true
      getLineList(this.listQuery).then(response => {
        this.lineOptions = response.data.lines
        this.listLoading = false
        if (this.lineOptions.length > 0) {
          // this.listQuery.lineNumber = this.lineOptions[0].lineNumber
          this.getList()
          this.getListEx()
        }
      })
    },
    // 点击搜索时获取清单
    handleFilter() {
      this.getList()
      this.getListEx()
    },
    // 获取清单
    getList() {
      this.listLoading = true
      getProcessList(this.listQuery).then(response => {
        this.list = response.data.processes
        this.total = response.data.total
        this.lineChartData.valueX = []
        this.lineChartData.valueY = []
        this.list.forEach(e => {
          this.lineChartData.valueX.push(e.processName)
          this.lineChartData.valueY.push(e.cycleTime)
        })
        this.listLoading = false
      })
    },
    // 获取清单
    getListEx() {
      this.listLoading = true
      getProcessCycle(this.listQuery).then(response => {
        var processes = response.data.processes
        this.total = response.data.total
        this.lineChartData2.valueX = []
        this.lineChartData2.valueY = []
        processes.forEach(e => {
          this.lineChartData2.valueX.push(e.processName)
          this.lineChartData2.valueY.push(e.cycleTime)
        })
        this.listLoading = false
      })
    },
    // 查看线体设备
    handleCheckLine(row) {
      const ret = Object.assign({}, row) // copy obj
      this.$router.push({ path: '/equipment/stationDef', query: { lineId: ret.lineId }})
    }
  }
}
</script>
<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  height: 800px;
  background-color: white;
  position: relative;

  .github-corner {
    position: absolute;
    top: 0px;
    border: 0;
    right: 0;
  }

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px x0;
    //margin-bottom: 32px;
  }
}

@media (max-width:1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
